package com.shujia.core

import org.apache.flink.streaming.api.scala._

object Demo1StreamWordCount {

  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //设置并行的数量
    //并行的由数据量
    env.setParallelism(2)

    //读取socket构建DS
    //nc -lk 8888
    val lineDS: DataStream[String] = env.socketTextStream("master",8888)

    //1、将单词拆分
    val wordDS: DataStream[String] = lineDS.flatMap(_.split(","))

    //2、转换成kv格式
    val kvDS: DataStream[(String, Int)] = wordDS.map((_,1))

    //3、按照key进行分组，底层也是hash分区
    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    //4、对value进行聚合
    val countDS: DataStream[(String, Int)] = keyByDS.sum(1)

    //打印结果
    countDS.print()

    //启动link程序
    env.execute()

  }

}
